package com.ddn.service.personnel.impl;

import com.ddn.mapper.personnel.PerfMapper;
import com.ddn.model.personnel.BarVO;
import com.ddn.model.personnel.Perf;
import com.ddn.model.personnel.PerfVO;
import com.ddn.service.personnel.PerfService;
import com.ddn.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author Bikakaso
 * @date 2023/1/2
 * ddn_hotel
 */
@Service
public class PerfServiceImpl implements PerfService {
    @Autowired
    private PerfMapper perfMapper;

    @Override
    public ResultData findAll(HashMap<String, Object> map) {
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        String ttt = (String) map.get("name");
        map.put("ttt",ttt);
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(limit));
        List<PerfVO> list = perfMapper.findAll(map);
        PageInfo<PerfVO> info = new PageInfo<>(list);
        if (list != null) {
            return ResultData.ok(list, info.getTotal());
        }
        return ResultData.fail();
    }

    @Override
    public ResultData deleteById(int id) {
        int i = perfMapper.deleteById(id);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData deleteBatch(int[] ids) {
        int i = perfMapper.deleteBatch(ids);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData add(HashMap<String,Object> map) {
        String emplId = (String) map.get("emplId");
        Integer ids = perfMapper.selectId(emplId);
        System.out.println("ids = " + ids);
        map.put("ids",ids);
        if (ids != null) {
            String month = (String) map.get("month");
            String year = (String) map.get("year");
            if (month == "") {
                month = "0.0";
                map.put("month",month);
            }
            if (year == "") {
                year = "0.0";
                map.put("year",year);
            }
            int i = perfMapper.add(map);
            if (i > 0) {
                return ResultData.ok();
            }
        }
        return ResultData.fail();
    }

    @Override
    public ResultData edit(HashMap<String,Object> map) {
        String month = (String) map.get("month");
        String year = (String) map.get("year");
        double v1 = Double.parseDouble(month);
        double v2 = Double.parseDouble(year);
        Double grade = v1*0.1 + v2 ;
        map.put("grade",grade);
        int i = perfMapper.edit(map);
        if (i > 0) {
            return ResultData.ok();
        }
        return ResultData.fail();
    }

    @Override
    public ResultData echartsBar() {
        List<BarVO> list = perfMapper.echartsBar();
        System.out.println("list = " + list);
        if (list != null) {
            return ResultData.ok(list);
        }
        return ResultData.fail();
    }
}
